﻿Public Class frmMercaderiaBuscador

    Private _codMercaderia As Integer = 0
    Public Property codMercaderia() As Integer
        Get
            Return _codMercaderia
        End Get
        Set(ByVal value As Integer)
            _codMercaderia = value
        End Set
    End Property

    Private _codMercaderiaLote As Integer = 0
    Public Property codMercaderiaLote() As Integer
        Get
            Return _codMercaderiaLote
        End Get
        Set(ByVal value As Integer)
            _codMercaderiaLote = value
        End Set
    End Property

    Private _codVenta As Integer = 0
    Public Property codVenta() As Integer
        Get
            Return _codVenta
        End Get
        Set(ByVal value As Integer)
            _codVenta = value
        End Set
    End Property

    Private _codCompra As Integer = 0
    Public Property codCompra() As Integer
        Get
            Return _codCompra
        End Get
        Set(ByVal value As Integer)
            _codCompra = value
        End Set
    End Property

    Private _codDeposito As Integer = 0
    Public Property codDeposito() As Integer
        Get
            Return _codDeposito
        End Get
        Set(ByVal value As Integer)
            _codDeposito = value
        End Set
    End Property

    Private _Mercaderia As String = ""
    Public Property MercaderiaDesc() As String
        Get
            Return _Mercaderia
        End Get
        Set(ByVal value As String)
            _Mercaderia = value
        End Set
    End Property

    Private _codigoBarra As String = ""
    Public Property CodigoBarra() As String
        Get
            Return _codigoBarra
        End Get
        Set(ByVal value As String)
            _codigoBarra = value
        End Set
    End Property

    Private _esVenta As Boolean = False
    Public Property esVenta() As Boolean
        Get
            Return _esVenta
        End Get
        Set(ByVal value As Boolean)
            _esVenta = value
        End Set
    End Property

    Private _esDevolucion As Boolean = False
    Public Property esDevolucion() As Boolean
        Get
            Return _esDevolucion
        End Get
        Set(ByVal value As Boolean)
            _esDevolucion = value
        End Set
    End Property

    Private Sub frmMercaderiaBuscador_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        dgMercaderia.AutoGenerateColumns = False
        dgMercaderiaLote.AutoGenerateColumns = False

        If esDevolucion Then
            dgMercaderiaLote.Visible = False
            dgMercaderia.Visible = True
        Else

            If esVenta Then
                dgMercaderiaLote.Visible = True
                dgMercaderia.Visible = False
            Else
                dgMercaderiaLote.Visible = False
                dgMercaderia.Visible = True
            End If

        End If

        CargarCombos()
        CargarMercaderias()
        dgMercaderia.Focus()

    End Sub

    Dim classController As New ControllerSigeco.controller_stock
    Dim classFacturaVenta As New ControllerSigeco.controller_ventas
    Dim classFacturaCompra As New ControllerSigeco.controller_compras

    Private Sub CargarMercaderias()
        Try

            btnBuscar.Enabled = True

            Dim idMarca As Integer = 0
            If cbxMarca.Checked Then
                idMarca = cboMarca.SelectedValue
            End If

            Dim idRubro As Integer = 0
            If cbxRubro.Checked Then
                idRubro = cboRubro.SelectedValue
            End If

            Dim ds As New DataTable

            If codVenta > 0 Then
                ds = classFacturaVenta.get_Ventas_Detalles_Devolucion(oiml.dbConn, codVenta)
            ElseIf codCompra > 0 Then
                ds = classFacturaCompra.get_Compras_Detalles_Devolucion(oiml.dbConn, codCompra)
            Else
                ds = classController.get_mercaderias_All(oiml.dbConn, 0, txtMercaderia.Text, idMarca, idRubro, esVenta, CodigoBarra, codDeposito, 0)
            End If

            If Not ds Is Nothing Then

                If esDevolucion Then

                    dgMercaderia.DataSource = ds
                    dgMercaderia.Focus()

                Else

                    If esVenta Then
                        dgMercaderiaLote.DataSource = ds
                        dgMercaderia.Focus()
                    Else
                        dgMercaderia.DataSource = ds
                    End If

                End If

            Else
                dgMercaderiaLote.DataSource = Nothing
                dgMercaderia.DataSource = Nothing
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub CargarCombos()
        Dim dsMarca As New DataTable
        dsMarca = classController.get_marcas(oiml.dbConn, 0, "")
        If Not dsMarca Is Nothing Then
            cboMarca.DataSource = dsMarca
            cboMarca.DisplayMember = "marca"
            cboMarca.ValueMember = "idMarca"
        End If

        Dim dsRubro As New DataTable
        dsRubro = classController.get_rubros(oiml.dbConn, 0, "")
        If Not dsRubro Is Nothing Then
            cboRubro.DataSource = dsRubro
            cboRubro.DisplayMember = "rubro"
            cboRubro.ValueMember = "idRubro"
        End If

        'Dim dsImpuesto As New DataTable
        'dsImpuesto = classController.get_impuestos(oiml.dbConn, 0, "")
        'If Not dsImpuesto Is Nothing Then
        '    cboImpuesto.DataSource = dsImpuesto
        '    cboImpuesto.DisplayMember = "impuesto"
        '    cboImpuesto.ValueMember = "idImpuesto"
        'End If

    End Sub

    Private Sub seleccionar()
        Try
            If esDevolucion Then

                If dgMercaderia.SelectedRows.Count > 0 Then
                    codMercaderia = dgMercaderia.SelectedRows(0).Cells(0).Value
                    MercaderiaDesc = dgMercaderia.SelectedRows(0).Cells(1).Value
                    codMercaderiaLote = dgMercaderia.SelectedRows(0).Cells(2).Value
                    Me.Close()
                End If

            Else

                If esVenta Then

                    If dgMercaderiaLote.SelectedRows.Count > 0 Then
                        codMercaderia = dgMercaderiaLote.SelectedRows(0).Cells(0).Value
                        MercaderiaDesc = dgMercaderiaLote.SelectedRows(0).Cells(1).Value
                        codMercaderiaLote = dgMercaderiaLote.SelectedRows(0).Cells(2).Value
                        Me.Close()
                    End If

                Else

                    If dgMercaderia.SelectedRows.Count > 0 Then
                        codMercaderia = dgMercaderia.SelectedRows(0).Cells(0).Value
                        MercaderiaDesc = dgMercaderia.SelectedRows(0).Cells(1).Value
                        codMercaderiaLote = dgMercaderia.SelectedRows(0).Cells(2).Value
                        Me.Close()
                    End If

                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        CargarMercaderias()

    End Sub

    Private Sub dgMercaderia_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgMercaderia.KeyDown
        If e.KeyCode = Keys.Enter Then
            seleccionar()
        End If
    End Sub

    Private Sub dgMercaderia_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgMercaderia.DoubleClick
        seleccionar()
    End Sub

    Private Sub dgMercaderiaLote_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgMercaderiaLote.KeyDown
        If e.KeyCode = Keys.Enter Then
            seleccionar()
        End If
    End Sub

    Private Sub dgMercaderiaLote_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgMercaderiaLote.DoubleClick
        seleccionar()
    End Sub

    Private Sub txtMarca_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMercaderia.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMercaderias()
        End If
    End Sub

End Class